#!/usr/bin/env python
import logging
from google.appengine.ext import db
import GeneralM
import TopicM
import LectureM
import QuestionM

def startClass(lectureKey):
    # TODO: adapt for multiple classes
    
    owner = GeneralM.getEmail()
    if not owner:
        return 'error: user is not logged in'
    # get lecture
    lecture=LectureM.getLectureByKey(lectureKey)
    
    # get first TopicM.Topic
    currentTopic=None
    TopicQuery=TopicM.getTopicsByLecture(lecture)
    if TopicQuery.count()>0:
        currentTopic=TopicQuery.get()
    
    classQuery=db.Query(ClassState).filter('lecture =',lecture)
#    count=classQuery.count()
#    if count > 0:
#        prevClass=classQuery.get()
#        index=(prevClass.index)+1
#    
    db.delete(classQuery)
#    if index<=0:
#        classQuery=db.Query(ClassState).filter('lecture =',lecture)
#        index=classQuery.count()+1
#    else:
#        db.delete(db.Query(ClassState).filter('lecture =',lecture).filter('index =', index))
    # TODO: currentTopicM.TopicTime
    
    # create and store the new classState object
    classState=ClassState(lecture=lecture,currentTopic=currentTopic)
    classState.put()
    
    return True

def setCurrentTopic(lectureKey , TopicKey):
    owner = GeneralM.getEmail()
    if not owner:
        return 'error: user is not logged in'
    # get classState 
    classS=getClassByLectureKey(lectureKey)
    
    # set current TopicM.Topic
    classS.currentTopic=TopicM.getTopicByKey(TopicKey)
    
    # store the updated classState object
    classS.put()
    
    return True

def getCurrentTopic(lectureKey):
#    owner = GeneralM.getEmail()
#    if not owner:
#        return 'error: user is not logged in'
    # get classState 
#    classS=getClassByLectureKey(lectureKey)
    
    # Bad Solution - Temporary
    lecture = LectureM.getLecture(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    
    # get current TopicM.Topic
    return classS.currentTopic

def getClassByLectureKey(lectureKey):
    owner = GeneralM.getEmail()
    lecture=LectureM.getLectureByKey(lectureKey)
    if owner and lectureKey:
        classS = db.Query(ClassState).filter('lecture =',lecture).get()
        if CourseM.isOwner(classS.lecture.course):
            logging.info('great!')
            return classS
        else:
            logging.info(classS.lecture.course.owner + ' !=  ' + owner)
            return classS
            #return 'error:user accessed lecture key of another user'
        
#def getClassIndex(lectureKey):
#    owner = GeneralM.getEmail()
#    lecture=LectureM.getLectureByKey(lectureKey)
#    if owner and lectureKey:
#        classS = db.Query(ClassState).filter('lecture =',lecture).get()
#        if classS:
#            if (classS and classS.lecture.course.owner == owner):
#                return classS.index
#            else:
#                return 'error:user accessed lecture key of another user'
#        else: return 'error'


# need improvement
def setQuizQuestion(questionKey, lectureKey):
    classS=getClassByLectureKey(lectureKey)
    
    question=None
    if questionKey:
        question=QuestionM.getQuestionByKey(questionKey)
        
    classS.quizQuestion=question
    classS.put()
    
    return True

def setEndQuiz(lectureKey):
    classS=getClassByLectureKey(lectureKey)
        
    classS.quizQuestion=None
    classS.put()
    
    return True
    
def getQuestionKey(lectureKey):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    return str(classS.quizQuestion.key())

def getQuestion(lectureKey):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    question = classS.quizQuestion
    if question:
        return classS.quizQuestion.name
    else:
        return None

def getAnswers(lectureKey):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    return classS.quizQuestion.answers

def isQuizOn(lectureKey):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    question = classS.quizQuestion
    if question:
        return classS.quizQuestion.key()
    else:
        return None

def getRightAnswer(lectureKey):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    question=classS.quizQuestion
    if question:
        return question.answers[question.rightAnswer]
    else:
        return None

def isCorrect(lectureKey, answerIndex):
    lecture = LectureM.Lecture.get(lectureKey)
    classS= db.Query(ClassState).filter('lecture =',lecture).get()
    question=classS.quizQuestion
    if question:
        return question.rightAnswer==int(answerIndex)
    else:
        return False

class ClassState(db.Model):
    lecture = db.ReferenceProperty(LectureM.Lecture)
    currentTopic = db.ReferenceProperty(TopicM.Topic)
    quizQuestion = db.ReferenceProperty(QuestionM.Question)
    startDate = db.DateTimeProperty(auto_now_add=True)

#    ended = db.BooleanProperty()
#    index=db.IntegerProperty()

import CourseM
